﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Communergy.Common;

namespace CommunergyLogicTests.MockObjects
{
    public class MockHistoricRepository : IHistoricRepository
    {
        MockUnitOfWork unitOfWork;
        public MockHistoricRepository(IUnitOfWork unitOfWork)
        {
            this.unitOfWork = unitOfWork as MockUnitOfWork;
        }

        #region IHistoricRepository Members

        public void Insert_ElectricityHourly(string meterId, List<HistoricalEnergyReading> hourlyEnergyReadings)
        {
            var mockHistoric = MockHistorical(meterId, hourlyEnergyReadings);
            unitOfWork.HourlyPending = unitOfWork.HourlyPending.Union(mockHistoric).ToList() ;
        }

        
        public void Insert_ElectricityDaily(string meterId, List<HistoricalEnergyReading> dailyEnergyReadings)
        {
            var mockHistoric = MockHistorical(meterId, dailyEnergyReadings);
            unitOfWork.DailyPending = unitOfWork.DailyPending.Union(mockHistoric).ToList();
        }

        public void Insert_ElectricityMonthly(string meterId, List<HistoricalEnergyReading> monthlyEnergyReadings)
        {
            var mockHistoric = MockHistorical(meterId, monthlyEnergyReadings);
            unitOfWork.MonthlyPending = unitOfWork.MonthlyPending.Union(mockHistoric).ToList() ;
        }

        public List<HistoricalEnergyReading> GetHourly(string meterId, DateTime dateFrom)
        {
            var mockHourly = unitOfWork.HourlyCommitted.Where(h => h.ReadingDateTime >= dateFrom && h.MeterId == meterId).ToList();
            var hourly = CleanMock(mockHourly);
            return hourly;
        }

        #endregion

        private List<HistoricalEnergyReading> CleanMock(List<MockHistorical> mockHistorical)
        {
            var clean = (from m in mockHistorical
                          select new HistoricalEnergyReading
                          {
                              KWh = m.KWh,
                              ReadingDateTime = m.ReadingDateTime
                          }).ToList();
            return clean;
        }

        private List<MockHistorical> MockHistorical(string meterId, List<HistoricalEnergyReading> historicalEnergyReadings)
        {
            var mockHistoric = (from r in historicalEnergyReadings
                                select new MockHistorical
                                {
                                    MeterId = meterId,
                                    ReadingDateTime = r.ReadingDateTime,
                                    KWh = r.KWh
                                }).ToList();
            return mockHistoric;
        }
        
    }
}
